<template>
	<view class="container">
		<view class="queryForm">
			<uni-forms ref="queryRef" :model="queryParams" label-width="70px" label-align="right">
				<uni-forms-item label="任务名称">
					<uni-easyinput v-model="queryParams.jobName" placeholder="请输入任务名称" />
				</uni-forms-item>
				<uni-forms-item label="任务组名">
					<xy-select v-model="queryParams.jobGroup" :data="sys_job_group" placeholder="任务组名" clearable />
				</uni-forms-item>
				<uni-forms-item label="任务状态" name="status">
					<xy-select v-model="queryParams.status" :data="sys_common_status" placeholder="任务状态" clearable />
				</uni-forms-item>
				<uni-forms-item label="操作">
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="handleQuery()">搜索</button>
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="resetQuery()">重置</button>
					<button class="mini-btn xy-btn" type="warn" size="mini" @click="handleClean()"
						v-if="proxy.$auth.hasPermi('monitor:job:remove')">清空</button>
				</uni-forms-item>
			</uni-forms>
		</view>
		<!-- @iconClick="iconClick" -->
		<view v-for="(item,index) in jobLogList" :key="index">
			<uni-card>
				<view class="uni-body">
					<view>
						<text>日志编号：</text>
						<text>{{ item.jobLogId }}</text>
					</view>
					<view>
						<text>任务名称：</text>
						<text>{{ item.jobName }}</text>
					</view>
					<view>
						<text>任务组名：</text>
						<dict-tag :options="sys_job_group" :value="item.jobGroup" />
					</view>
					<view>
						<text>调用目标字符串：</text>
						<text>{{ item.invokeTarget }}</text>
					</view>
					<view>
						<text>日志信息：</text>
						<text>{{ item.jobMessage }}</text>
					</view>
					<view>
						<text>执行状态：</text>
						<dict-tag :options="sys_common_status" :value="item.status" />
					</view>
					<view>
						<text>执行时间：</text>
						<text>{{ item.createTime }}</text>
					</view>
					<view>
						<text>异常信息：</text>
						<text v-if="item.status == 1">{{ form.exceptionInfo }}</text>
					</view>
					<view slot="actions" class="card-actions">
						<button class="mini-btn xy-btn" type="warn" size="mini" @click="handleDelete(item)"
							v-if="proxy.$auth.hasPermi('monitor:job:remove')">删除</button>
					</view>
				</view>
			</uni-card>
		</view>
		<uni-load-more :status="xlstatus" />
	</view>
</template>

<script setup>
	import {
		getJob
	} from "@/pages_work/api/monitor/job";
	import {
		listJobLog,
		delJobLog,
		cleanJobLog
	} from "@/pages_work/api/monitor/jobLog";
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		onLoad,
		onPullDownRefresh,
		onReachBottom,
	} from '@dcloudio/uni-app'
	const {
		proxy
	} = getCurrentInstance();
	const {
		sys_job_group,
		sys_common_status
	} = proxy.useDict("sys_job_group", "sys_common_status");

	/** 查询调度日志列表所需的参数 */
	const queryParams = ref({
		pageNum: 1,
		pageSize: 10,
	});
	/** 总数 */
	const total = ref(0);
	/** 调度日志列表 */
	const jobLogList = ref([]);
	/** 下拉状态 */
	const xlstatus = ref('more');

	onLoad((params) => {
		uni.setNavigationBarTitle({
			title: params.title
		});
		if (params.jobId == undefined || params.jobId == null || params.jobId == '') {
			handleQuery();
		} else {
			getJob(params.jobId).then(response => {
				queryParams.value.jobName = response.data.jobName;
				queryParams.value.jobGroup = response.data.jobGroup;
				handleQuery();
			});
		}
	})

	/** 查询调度日志列表 */
	const getList = () => {
		listJobLog(queryParams.value).then(res => {
			jobLogList.value = jobLogList.value.concat(res.rows);
			total.value = res.total;

			if (res.total == undefined) {
				// 标记加载数据完成
				xlstatus.value = 'more';
			} else if (res.total <= jobLogList.value.length) {
				// 标记加载数据完成
				xlstatus.value = 'noMore';
			} else {
				// 标记还有剩余数据
				xlstatus.value = 'more';
			}
		})
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		xlstatus.value = 'more';
		queryParams.value.pageNum = 1;
		jobLogList.value = [];
		getList();
	};

	/** 重置按钮操作 */
	function resetQuery() {
		// dateRange.value = [];
		proxy.resetForm("queryRef");
		queryParams.value = {
			pageNum: 1,
			pageSize: 10,
		};
		handleQuery();
	};

	/** 删除按钮操作 */
	const handleDelete = (row) => {
		const ids = row.jobLogId;
		proxy.$modal.confirm('是否确认删除调度日志编号为"' + ids + '"的数据项？').then(function() {
			return delJobLog(ids);
		}).then(() => {
			handleQuery();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	};

	/** 清空按钮操作 */
	function handleClean() {
		proxy.$modal.confirm("是否确认清空所有调度日志数据项?").then(function() {
			return cleanJobLog();
		}).then(() => {
			handleQuery();
			proxy.$modal.msgSuccess("清空成功");
		}).catch(() => {});
	}

	/** 生命周期函数--下拉刷新 */
	onPullDownRefresh(() => {
		handleQuery()
		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 1000)
	})

	/** 生命周期函数-上拉加载 */
	onReachBottom(() => {
		if (xlstatus.value == 'noMore' || xlstatus.value == 'loading') {
			// 如果没有更多数据或者正在加载数据，则不需要加载更多，直接返回
			return;
		}
		// 标记正在加载数据
		xlstatus.value = 'loading';
		queryParams.value.pageNum++;
		getList();
	})
</script>

<style lang="scss">
	@import "@/static/scss/work/index.scss";
</style>